package com.quanfaqiang.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanfaqiang.demo1.mapper.*;
import com.quanfaqiang.demo1.pojo.*;
import com.quanfaqiang.demo1.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FurnitureMapper furnitureMapper;
    @Autowired
    private QuyutypesMapper quyutypesMapper;
    @Autowired
    private FurnituretypesMapper furnituretypesMapper;

    @Override
    public Page<Task> SelectAll(int currentPage, int pageSize) {
        Page<Task> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
        Page<Task> taskPage = taskMapper.selectPage(page, taskQueryWrapper);
        List<Task> records = taskPage.getRecords();
        for (Task record : records) {
            User user = userMapper.selectById(record.getUserId());
            record.setUser(user);
            Furniture furniture = furnitureMapper.selectById(record.getFurnitureId());
            Quyutypes quyutypes = quyutypesMapper.selectById(furniture.getQuyuTypesId());
            furniture.setQuyuTypes(quyutypes.getName());
            Furnituretypes furnituretypes = furnituretypesMapper.selectById(furniture.getFurnitureTypesId());
            furniture.setFurnitureTypes(furnituretypes.getName());
            record.setFurniture(furniture);

        }
        return taskPage;
    }

    @Override
    public void DeleteId(Integer id) {
        taskMapper.deleteById(id);

    }

    @Override
    public void deleteIds(List<Integer> ids) {
        taskMapper.deleteBatchIds(ids);

    }

    @Override
    public Task SelectId(Integer id) {
        Task task = taskMapper.selectOne(new QueryWrapper<Task>().eq("id", id));
        return task;
    }

    @Override
    public void Add(Task task) {
        String uuid = UUID.randomUUID().toString();// 从UUID中提取数字部分
        String digits = uuid.replaceAll("[^0-9]", "");
        while (digits.length() < 6) {
            uuid = UUID.randomUUID().toString();
            digits = digits + uuid.replaceAll("[^0-9]", "");
        }
        String sixDigitNumber = digits.substring(0, 6);
        task.setUuidNumber(Integer.parseInt(sixDigitNumber));
        task.setState(false);
        System.out.println(task + "------------");
        taskMapper.insert(task);
    }

    @Override
    public void update(Task task) {
        taskMapper.updateById(task);
    }

    @Override
    public Boolean selectTaskName(String name, Integer id) {
        UpdateWrapper<Task> taskUpdateWrapper = new UpdateWrapper<>();
        taskUpdateWrapper.eq("id", id);
        taskUpdateWrapper.eq("name", name);
        Task task = taskMapper.selectOne(taskUpdateWrapper);
        if (task != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Task selectUuidNumber(Integer uuidNumber) {
        QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
        taskQueryWrapper.eq("uuid_number", uuidNumber);
        Task task = taskMapper.selectOne(taskQueryWrapper);
        return task;
    }

    @Override
    public Page<Task> serchTask(Integer uuidNumber, String name, Integer currentPage, Integer pageSize, String username, Integer number, String content) {
        Page<Task> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
        if (uuidNumber != null && !"".equals(uuidNumber)) {
            taskQueryWrapper.like("uuid_number", uuidNumber);
        }
        if (name != null && !"".equals(name)) {
            taskQueryWrapper.like("name", name);
        }
        if (content != null && !"".equals(content)) {
            taskQueryWrapper.like("content", content);
        }
        if (username != null && !"".equals(username)) {
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
            if (user != null) {
                taskQueryWrapper.eq("user_id", user.getId());
            }
        }
        if (number != null && !"".equals(number)) {
            Furniture furniture = furnitureMapper.selectOne(new QueryWrapper<Furniture>().eq("number", number));
            if (furniture != null) {
                taskQueryWrapper.eq("furniture_id", furniture.getId());
            }
        }
        Page<Task> taskPage = taskMapper.selectPage(page, taskQueryWrapper);
        List<Task> records = taskPage.getRecords();
        for (Task record : records) {
            User user = userMapper.selectById(record.getUserId());
            record.setUser(user);
            Furniture furniture = furnitureMapper.selectById(record.getFurnitureId());
            Quyutypes quyutypes = quyutypesMapper.selectById(furniture.getQuyuTypesId());
            furniture.setQuyuTypes(quyutypes.getName());
            Furnituretypes furnituretypes = furnituretypesMapper.selectById(furniture.getFurnitureTypesId());
            furniture.setFurnitureTypes(furnituretypes.getName());
            record.setFurniture(furniture);
        }
        return taskPage;

    }

    @Override
    public Boolean updateState(Integer id, boolean state) {
        UpdateWrapper<Task> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("id", id).set("state", state);
        int update = taskMapper.update(objectUpdateWrapper);
        if (update != 0 || update > 0) {
            return true;
        } else {
            return false;
        }
    }
}
